package com.feige.service.impl;



import com.feige.pojo.SecurityUser;
import com.feige.pojo.User;
import com.feige.openfeigh.UserService;
import com.feige.result.AjaxResult;
import com.feige.result.R;
import com.feige.utils.exception.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author feige
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        R<User> r = userService.getUserByEmail(s);
        User user;
        if (r.getCode() == 200){
            user = r.getData();
        }else {
            throw new CustomException("登录失败，请重试或联系管理员！",5050);
        }
        if (user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        SecurityUser securityUser = new SecurityUser();
        BeanUtils.copyProperties(user,securityUser);
        return securityUser;
    }
}
